package com.lujianfei.android_app_list.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

abstract class BaseFragment: Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(resourceId(),container,false)
    }

    fun <T:View> findViewById(id:Int):T? {
        return view?.findViewById(id)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initView()
        initData()
        initEvent()
    }

    abstract fun resourceId(): Int

    abstract fun initView()

    abstract fun initData()

    abstract fun initEvent()

    protected fun launchOnUI(block:suspend CoroutineScope.()->Unit)
            = CoroutineScope(Dispatchers.Main).launch { block() }
}